/* 
	LMtmr_Set_Timer 				1 unit = 625us
	rdabt_timer_create		  		1 unit = 100ms
	
	Notice: tports, watch clock (0x40200000,0x44210aaa);
	
	
*/

#include "sys_config.h"
#include "sys_types.h"

#include "bt_types.h"
#include "host_config.h"
#include "papi.h"
#include "hid.h"
#include "hid_wii.h"
#include "hid_main.h"
#include "hs_battery.h"
#include "hw_i2c_driver.h"
#include "hw_delay.h"
#include "lmp_scan.h"
#include "hid_core.h"
#include "tra_combined.h"
#include "lmp_utils.h"
#include "lmp_timer.h"
#include "Manager.h"
#include "mgr_link.h"
#include "hw_apb_spi.h"
#include "patch.h"
#include "lmp_link_key_db.h"
#include "sdp_db.h"
#include "spp_dun.h"
#include "sys_rda_arm.h"

#include "hfp.h"


#define LED_ON  			   LED_ALWAYS_DARK//LED_ALWAYS_LIGHT
#define LED_OFF				   LED_ALWAYS_LIGHT//LED_ALWAYS_DARK



#define LED_Power              1
#define LED_Pair               0
#define WP_IIC_PIN             2
#define NOUSE				   				 3
#define LED_CHARGE       NOUSE
#define LED_CAPS			   NOUSE





/* for hid_config_eeprom */
#define HID_DEBUG_UART                      0x01
#define HID_DEBUG_CUSTOMER					0x02
#define HID_DEBUG_SLEEP					    0x04
#define HID_DEBUG_LOW_POWER					0x08
#define HID_DEBUG_SNIFF_3					0x10
//#define HID_DEBUG_SPP						0x20
#define HID_TEST_SPP						0x20


extern u_int8   gLinkIndex;
extern u_int8   hFifoIndex;
extern u_int8   ghid_fifo[24];
extern u_int8  reportFlag;
extern t_hid_pskey  hid_config_eeprom;
extern HID   ghid;
extern u_int8 hid_led_num[4];
extern u_int16     gHidHandle;
extern t_lmp_link *hid_link;
extern t_SYS_Config g_sys_config;

extern u_int32 sdp_replace_data[8];
extern const t_SDP_serviceRecord sdp_spp_service;
extern t_FHSpacket LC_local_device_fhs;
extern u_int8 isSaveKey;



extern void HSMain_InitSleepMode(void *dummy);


extern u_int8 LMtmr_Set_Timer(t_slots timer_value,void ((*p_funct)(t_lmp_link*)), t_lmp_link* p_link, u_int8 dynamic);
extern void _HID_INT_Disable(void);
extern  u_int8 LMkeydb_EEprom_Load_Addr_link_Key(u_int16 baseAddr);
extern  void HID_Init_Keyboard(void);
extern void _HID_INT_Enable(void);
extern void _Set_PWM_Ratio(u_int8  mode,u_int16 initial,u_int16 toggle);

#if 0
extern const t_SDP_attribute attribute_hsp[7];
extern const t_SDP_attribute attribute_hfp[7];

const t_SDP_serviceRecord sdp_p_hfp_service = 
	{sizeof(attribute_hfp)/sizeof(attribute_hfp[0]),    attribute_hfp};

const t_SDP_serviceRecord sdp_p_hs_service = 
	{sizeof(attribute_hsp)/sizeof(attribute_hsp[0]),    attribute_hsp};
#endif



// Notice: Word Alignment
const u_int8 hid_bd_addr[8] = {0xbb, 0x99, 0x77, 0x55, 0x33, 0x11,0x00,0x00}; //  addr


/* The fuction replaces 'inline' FHS_Set_Device_Class for patch compile*/
void p_Set_Device_Class(t_FHSpacket *fhs, t_classDevice class_of_device)
{
    u_int8 *p_fhs_device_class = fhs->payload+11;
    *p_fhs_device_class++ = (u_int8) (class_of_device & 0xFF);
    class_of_device >>=8;
    *p_fhs_device_class++ = (u_int8) (class_of_device & 0xFF);
    class_of_device >>=8;
    *p_fhs_device_class++ = (u_int8) (class_of_device & 0xFF);
}



void p_hs_init()
{
	u_int8  tmp,i;
	
	ghid.pinCodeStatus = HID_PINCODE_IDLE;

	for(i=0;i<4;i++)
		hid_led_num[i] = NOUSE;
	
	

	LMconfig_LM_Change_Local_Name((u_int8 *)"Test HS");   
	// Set COD
    g_sys_config.device_class = 0x200404;  // headset:0x200404
	p_Set_Device_Class(&LC_local_device_fhs, g_sys_config.device_class);
	
    rdabt_sdp_replace_service(0x1000a, NULL); // remove hid sdp record
    rdabt_sdp_replace_service(0x1000b, NULL); // remove hid pnp sdp record
    rdabt_sdp_replace_service(0x10007, NULL); // remove spp sdp record

	 //rdabt_sdp_replace_service(0x10000, &sdp_p_hfp_service); // restore hfp sdp record
	//rdabt_sdp_replace_service(0x10001, &sdp_p_hs_service); // restore hs sdp record
    

	

	HF_Activate(ACTIVE_PROFILE_HANDSFREE); 
	HF_Activate(ACTIVE_PROFILE_HEADSET); 

		/* disable ACL edr  */
	g_sys_config.feature_set[3] = 0xc8;
	/* disable ESCO edr  */
	g_sys_config.feature_set[5] &= 0x18;

	//pin code
	 g_sys_config.feature_set[6] = 0x51;    
	  g_sys_config.feature_set[7] = 0x00;   

	// SSP
    //g_sys_config.feature_set[6] = 0x59;    
    //g_sys_config.feature_set[7] = 0x87;  

	// slow flash
	HID_WRITE_REG_UINT32(0xf0,0xfa00);
	HID_WRITE_REG_UINT32(0xf8,0xfcff);
	//quick flash
	HID_WRITE_REG_UINT32(0xf4,0xfe00);
	HID_WRITE_REG_UINT32(0xfc,0xff00);

	HID_WRITE_REG_UINT32(0x30,0x800000);  	// clk_sel = fast  
	HID_WRITE_REG(0xe8,0xf); // Pwm_led_en		
	HID_WRITE_REG_UINT32(0x08,0x0f008000);	// PWM output

	
	for(i=0;i<4;i++)
	HID_Set_LED(i,LED_OFF,0x7fff); 

   	HID_Set_LED(2,LED_ON,0x7fff); 
	HID_Set_LED(1,LED_FLASH,0x7fff); 

	// the page size of C42A & C64A is 32-byte.
	hid_config_eeprom.linkeyAddr = 0x1fc0;
	gLinkIndex = LMkeydb_EE_Load_Link();	

		
	// start the connection
	if(gLinkIndex != 0xff){		
		//HF_Connect_SLC(ghid.address,ACTIVE_PROFILE_HANDSFREE);
	}

	

	LMscan_Write_Scan_Enable(BOTH_SCANS_ENABLED);

	HSBattery_Initialise(); // Notice: impact on the scan
	RDA_WRITE_REG_BYTE(RDA_APB_PMU_BASE+0x10c,0x30);  // disable HW control LED
	RDA_WRITE_REG_BYTE(RDA_APB_PMU_BASE+0x10b,RDA_READ_REG_BYTE(RDA_APB_PMU_BASE+0x10b)&0xfe);  // Disable the Shutdown of Charge
	RDA_WRITE_REG_BYTE(RDA_APB_PMU_BASE+0x1a7,1); // 26M stablize time : 1ms

}


void p_hs_lm_connected(u_int8 result,u_int16 handle)
{


}

void p_hs_disconnect(u_int8 result)
{


}











 
